package com.educate.serviceedu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.educate.serviceedu.entity.EduSubject;
import com.educate.serviceedu.entity.excel.SubjectData;
import com.educate.serviceedu.entity.subject.OneSubject;
import com.educate.serviceedu.entity.subject.TwoSubject;
import com.educate.serviceedu.listener.SubjectExcelListener;
import com.educate.serviceedu.mapper.EduSubjectMapper;
import com.educate.serviceedu.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author educate.wht
 * @since 2022-08-15
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {
        try{
            //文件输入流
            InputStream in=file.getInputStream();
            //调取方法进行读取
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();

        }catch (Exception e){
            e.printStackTrace();

        }


    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //查出所有一级分类 parent_id=0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);
        //查出所有二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);
        //创建list集合
        ArrayList<OneSubject> finalSubjectList = new ArrayList<>();
        //3封装一级分类
        //查询出来所有的一级分类list集合遍历,得到每个一级分类对象,获取每个一级分类对象值
        //封装到要求的list集合里面List<OneSubject>finalSubjectList
        for(int i=0;i<oneSubjectList.size();i++){
            //得到oneSubjectList每个eduSubject对象
            EduSubject eduSubject = oneSubjectList.get(i);
            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            BeanUtils.copyProperties(eduSubject,oneSubject);
            finalSubjectList.add(oneSubject);

            //在一级分类循环遍历查询所有的二级分类
            //创建list集合封装每个一级分类的二级分类
            List<TwoSubject> twoFinalSubjectList=new ArrayList<>();
            for (int m=0;m<twoSubjectList.size();m++) {
                //获取二级分类
                EduSubject tSubject = twoSubjectList.get(m);
                //判断二级分类的id是否和一级分类的id是否一致
                if(tSubject.getParentId().equals(eduSubject.getId())){
                    TwoSubject twoSubject=new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    twoFinalSubjectList.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoFinalSubjectList);
        }
        return finalSubjectList;
    }

//    @Override
//    public EduSubject getOne(QueryWrapper<SubjectData> wrapper) {
//        return null;
//    }
}
